import { getWorkTask } from '@/api/flow/work';
import { Modal } from 'ant-design-vue';

export default {
  data() {
    return {
      // nodeStatus
      // 0 === 未发起
      // 1 === 机审/开始
      // 2 === 机审/结束
      // 3 === 人工审核/开始
      // 4 === 人工审核/结束
      // 5 === 结束
      nodeStatus: 0,

      // fields promise
      workTaskPromise: null,

      timerState: {
        time: 5,
        example: null
      }
    };
  },

  computed: {
  },

  created() {
    this.workTaskPromise = this.getWorkTaskStatus();
  },

  beforeDestroy() {
    window.clearInterval(this.timerState.example);
  },

  methods: {
    okGoBack() {
      this.loanModel.visible = true;
      window.clearInterval(this.timerState.example);

      this.$router.push('/');
    },

    // loan success
    loanSuccess() {
      this.loanModel.visible = true;
      this.timerState.example = setInterval(() => {
        const { time } = this.timerState;
        if (time <= 0) {
          this.okGoBack();

          return false;
        }

        this.timerState.time -= 1;
      }, 1000);
    },

    // getWorkTaskStatus
    getWorkTaskStatus() {
      return new Promise((resolve, reject) => {
        getWorkTask(this.tid)
          .then((data) => {
            const current_node = data.current_nodes[0] || {};
            this.nodeStatus = current_node.name === '发起申请';

            if (!this.nodeStatus) {
              // this.loanSuccess();
            }
            resolve(data);
          })
          .catch((error) => reject(reject));
      });
    }
  }
};
